<template>
  <el-form
    ref="ruleForm"
    :rules="rules"
    :model="ruleForm"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="姓名" prop="username">
      <el-input
        v-model="ruleForm.username"
        style="width: 50%"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input
        v-model="ruleForm.mobile"
        style="width: 50%"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item label="入职日期" prop="timeOfEntry">
      <el-col style="width: 50%">
        <el-form-item prop="date1">
          <el-date-picker
            v-model="ruleForm.timeOfEntry"
            type="date"
            placeholder="选择入职日期"
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select
        v-model="ruleForm.formOfEmployment"
        placeholder="请选择"
        style="width: 50%"
      >
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />

      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input
        v-model="ruleForm.workNumber"
        style="width: 50%"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="ruleForm.departmentName"
        style="width: 50%"
        placeholder="请输入部门名称"
        @focus="departmentNameFocus"
      />
      <div v-show="showTree">
        <el-tree
          :data="list"
          :props="defaultProps"
          @node-click="nodeClickFn"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正日期" prop="correctionTime">
      <el-col style="width: 50%">
        <el-form-item prop="date1">
          <el-date-picker
            v-model="ruleForm.correctionTime"
            type="date"
            placeholder="选择转正日期"
            style="width: 100%"
          />
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">确认</el-button>
      <el-button @click="offFn">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import EmployeeEnum from '@/constant/employees' // 引入枚举
import { NewListToTreeData } from '@/utils/index' // 引入数组转树的文件
import { departmentsAPI } from '@/api/departments' // 请求数据
import { addEmployeeAPI } from '@/api/employees'
export default {
  data() {
    return {
      // 控制请求回来的数据显示或者隐藏
      showTree: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 定义一个空数组用于接收请求回来的部门列表
      list: [],
      // 定义一个变量接收一下枚举的数据
      hireType: EmployeeEnum.hireType,
      // 输入框定义的变量,根据后端接口文档来取的名字
      ruleForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入名字', trigger: ['blur', 'change'] },
          { min: 2, max: 3, message: '用户名为2-3个字', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '请输入聘用方式', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { type: 'date', required: true, message: '请选择入职时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 清空校验结果和数据 点击关闭按钮(x,取消,确认)
    closeFn() {
      // 清空数据
      // this.ruleForm.username = ''
      // this.ruleForm.mobile = ''
      // this.ruleForm.formOfEmployment = ''
      // this.ruleForm.workNumber = ''
      // this.ruleForm.departmentName = ''
      // this.ruleForm.timeOfEntry = ''
      // this.ruleForm.correctionTime = ''
      // 清空校验结果
      // this.$nextTick(() => {
      //   this.$refs.ruleForm.clearValidate()
      // })

      this.ruleForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.ruleForm.resetFields()
    },
    // 当被选中的部门名字被点击时,执行的方法
    nodeClickFn(data, node) {
      console.log(data, node)
      console.log('当前选中的部门是', data.name)
      // 如果有子元素的话,当前这个元素不能被选中,趁早返回
      if (data.children.length) {
        return
      }
      // 选中之后,把选中的值同步到输入框中
      this.ruleForm.departmentName = data.name
      // 输入框选中之后,隐藏tree
      this.showTree = false
    },
    // 部门获取焦点的时候获取列表,请求数据
    async departmentNameFocus() {
      const res = await departmentsAPI()
      console.log('请求回来的数据', res)
      // 把平铺数组转成树状结构
      const list = NewListToTreeData(res.depts)
      this.list = list
      this.showTree = true
    },
    // 点击取消按钮,关闭弹窗
    offFn() {
      this.$emit('offFn')
    },
    // 点击确定按钮,添加数据
    async addFn() {
      try {
        const res = await addEmployeeAPI(this.ruleForm)
        console.log('后端返回的值', res)
        this.$message.success('添加成功')
        // 关闭弹窗
        this.$emit('offFn')
        // 重新请求数据
        this.$emit('addEmployee')
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 关闭确定按钮
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          return
        }
        this.addFn()
        // 最简单的方法   valid && this.addFn()
      })
    }
  }
}
</script>

<style>
</style>
